﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data.SqlClient;
using System.Data;

namespace Acms.SqlDal
{
    public class TemplateHierarchyDal : Base
    {
        public enum Fields
        {
            Id,
            ParentId,
            ParentName,
            ChildId,
            ChildName,
            Sequence,
        }

        protected override void ReadRecord(System.Collections.Hashtable table, System.Data.SqlClient.SqlDataReader reader)
        {
            GetReaderValue<int>(Fields.Id, reader, table);
            GetReaderValue<int>(Fields.ParentId, reader, table);
            GetReaderValue<string>(Fields.ParentName, reader, table);
            GetReaderValue<int>(Fields.ChildId, reader, table);
            GetReaderValue<string>(Fields.ChildName, reader, table);
            GetReaderValue<int>(Fields.Sequence, reader, table);
        }

        public Hashtable GetById(int id)
        {
            SqlCommand cmd = CreateCommand("GetById");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            return base.ExecuteGetItem(cmd);
        }

        public Hashtable[] GetAllByChildId(int childId)
        {
            SqlCommand cmd = CreateCommand("GetAllByChildId");
            AddInputParameter(cmd, "ChildId", childId, SqlDbType.Int);
            return base.ExecuteGetCollection(cmd);
        }

        public Hashtable[] GetAllByParentId(int parentId)
        {
            SqlCommand cmd = CreateCommand("GetAllByParentId");
            AddInputParameter(cmd, "ParentId", parentId, SqlDbType.Int);
            return base.ExecuteGetCollection(cmd);
        }

        public void Update(ref int id, int parentId, ref string parentName, int childId, ref string childName, ref int sequence, ref object concurencyId)
        {
            SqlCommand cmd = CreateCommand("Put");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            AddInputParameter(cmd, "ParentId", parentId, SqlDbType.Int);
            AddInputParameter(cmd, "ChildId", childId, SqlDbType.Int);
            AddInputParameter(cmd, "Sequence", sequence, SqlDbType.Int);
            AddInputParameter(cmd, "ConcurencyId", concurencyId, SqlDbType.Timestamp);
            AddOutputParameter(cmd, "IdOut", SqlDbType.Int);
            AddOutputParameter(cmd, "ParentNameOut", SqlDbType.VarChar, 255);
            AddOutputParameter(cmd, "ChildNameOut", SqlDbType.VarChar, 255);
            AddOutputParameter(cmd, "SequenceOut", SqlDbType.Int);
            AddOutputParameter(cmd, "ConcurencyIdOut", SqlDbType.Timestamp);

            ExecuteCommand(cmd);

            id = GetOutputValue<int>(cmd, "IdOut");
            parentName = GetOutputValue<string>(cmd, "ParentNameOut");
            childName = GetOutputValue<string>(cmd, "ChildNameOut");
            sequence = GetOutputValue<int>(cmd, "SequenceOut");
            concurencyId = GetOutputValue<byte[]>(cmd, "ConcurencyIdOut");
        }

        public void Delete(int id, object concurencyId)
        {
            SqlCommand cmd = CreateCommand("Delete");
            AddInputParameter(cmd, "Id", id, SqlDbType.Int);
            AddInputParameter(cmd, "ConcurencyId", concurencyId, SqlDbType.Timestamp);
            ExecuteCommand(cmd);
        }
    }
}
